import os
import shutil
import subprocess
from datetime import datetime, timedelta

# server_user="ubuntu"
# server_ip="159.75.180.229"
# backup_user="wlc"
# backup_ip=""

server_folder="ubuntu@159.75.180.229:/home/ubuntu/.syncserver"
local_folder="/data/syncserver"

backup_folder=local_folder+"_bk"

today = datetime.now().strftime("%Y%m%d")
oldest_date = (datetime.now() - timedelta(days=7)).strftime("%Y%m%d") 

# 删除七天前的旧数据
for dir_time in os.listdir(backup_folder):
    print(dir_time)
    if dir_time < oldest_date:
        shutil.rmtree(os.path.join(backup_folder, dir_time))

# 今天的备份文件夹名
backup_today_folder = os.path.join(backup_folder, today)
os.makedirs(backup_today_folder, exist_ok=True)

subprocess.run(["rsync", "-avz", "--delete", local_folder, backup_today_folder])

subprocess.run(["rsync", "-avz", "--delete", server_folder, local_folder])
